from rest_framework import serializers

from goods.models import FoodCategory, Foods

from the_flash.settings import PICPATH


class FoodCutSerializer(serializers.ModelSerializer):

    class Meta:
        model = Foods
        fields = ['food_name', 'info', 'cover', 'food_id', 'shell_price',
                  'origin_price', 'norms', 'like_num', 'month_sales']

    def to_representation(self, instance):
        data = super().to_representation(instance)
        data['cover'] = PICPATH + '/' + data['cover'] + '.jpg'
        return data


class FoodCategorySerializer(serializers.ModelSerializer):

    class Meta:
        model = FoodCategory
        fields = ['food_category_id', 'food_category_name']

    def to_representation(self, instance):
        data = super().to_representation(instance)
        data['food_list'] = FoodCutSerializer(instance.foods_set.all(), many=True).data
        return data


class FoodSerializer(serializers.ModelSerializer):

    class Meta:
        model = Foods
        fields = '__all__'

    def to_representation(self, instance):
        data = super().to_representation(instance)
        data['cover'] = PICPATH + '/' + data['cover'] + '.jpg'
        del data['created_time']
        del data['status']
        del data['discount']
        del data['limit_num']
        return data
